package com.antler.smt.portal.controller;

import com.antler.smt.entity.QuestionnaireEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.portal.mapper.QuestionnaireEntityMapper;
import com.antler.smt.request.portal.QuestionReq;
import com.antler.smt.response.base.Result;
import com.antler.smt.utils.DateUtil;
import com.antler.smt.validate.DataValidator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 问卷调查接口
 *
 * @author zhangyi
 * @date 2018/9/19 15:38
 */
@RestController
@RequestMapping("/question")
@Api(description = "问卷调查接口")
public class QuestionnaireController extends BaseController{

    @Resource
    private QuestionnaireEntityMapper questionnaireEntityMapper;

    /**
     * 提交问卷调查
     *
     * @param questionReq
     * @return
     */
    @PostMapping("/submit")
    @ApiOperation(value = "提交问卷调查",httpMethod = "POST")
    public Result submit(@RequestBody QuestionReq questionReq){
        DataValidator.isNull(questionReq,"不能提交为空！");
        Long id = currentMember.getId();
        if(id != null){
            QuestionnaireEntity question = new QuestionnaireEntity();
            question.setMemberid(id);
            question.setSource(questionReq.getSource());
            question.setCreateTime(DateUtil.stampToDate(System.currentTimeMillis()));
            int i = questionnaireEntityMapper.insertSelective(question);
            return i != 0 ?new Result(ResultCodeEnum.OPERATION_SUCCESS,"提交成功！")
                    :new Result(ResultCodeEnum.APPEND_DATA_ERROR,"提交失败！");
        }
        return null;
    }
}
